﻿using System.Windows;
using System.Windows.Media;
using uTrade.Core;

namespace uTrade.Controls
{
    public class ContinuousLine : DrawObjBase
    {
        public DataSeries Data;

        protected override int TotalItemCount { get => Data.Count; }

        public Pen LinePen = new Pen(Brushes.BlueViolet, 2);

        //绘制连续的曲线
        protected override void DrawObj(DrawingContext dc)
        {
            // 1.Get Max and min
            double highest = 0;
            double lowest = double.MaxValue;

            var h = Data.Highest(drawItemStartIndex, drawItemCount);
            if (h > highest)
            {
                highest = h;
            }
            var l = Data.Lowest(drawItemStartIndex, drawItemCount);
            if (l < lowest)
            {
                lowest = l;
            }

            var Vals = Data;
            var pen = LinePen;

            PathFigure pfFast = new PathFigure();
            PathGeometry pgFast = new PathGeometry();
            pgFast.Figures.Add(pfFast);

            for (var i = 0; i < drawItemCount; ++i)
            {
                var itemIndex = drawItemStartIndex + i;
                var xoffset = (int)(i * (itemWidth + itemSpace)) + 0.5 + ChartStartX;
                var yoffset = (int)((highest - Vals[itemIndex]) / (highest - lowest) * ChartHeight) + 0.5 + ChartStartY;

                if (i == 0)
                {
                    pfFast.StartPoint = new Point(xoffset, yoffset);
                }
                else
                {
                    pfFast.Segments.Add(new LineSegment(new Point(xoffset, yoffset), true));
                }
            }
            dc.DrawGeometry(System.Windows.Media.Brushes.Transparent, pen, pgFast);
        }
    }
}